package jhunter.dao.dialect;

import java.util.List;

import jhunter.dao.bo.BaseObject;
import jhunter.dao.page.Page;

public class OracleDialect implements Dialect {

	@Override
	public String page(Page<BaseObject> page, String sql, List<Object> params) {
		sql = sql.trim();
		StringBuilder pagingSelect = new StringBuilder(sql.length() + 100);
		pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
		pagingSelect.append(sql);
		pagingSelect.append(" ) row_ where rownum <= ?) where rownum_ > ?");

		appendSqlParameters(page, params);

		return pagingSelect.toString();
	}

	protected void appendSqlParameters(Page<BaseObject> page, List<Object> params) {
		params.add(getFirstParameter(page));
		params.add(getSecondParameter(page));
	}

	@Override
	public String getHibernateDialectClassName() {
		return "org.hibernate.dialect.Oracle10gDialect";
	}

	@Override
	public int getFirstParameter(Page<BaseObject> page) {
		return page.getPageSize() * page.getPageNo();
	}

	@Override
	public int getSecondParameter(Page<BaseObject> page) {
		return page.getPageSize() * (page.getPageNo() - 1);
	}

}
